﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Job.Web.Framework;
using Job.Logic.Personal;
using Job.Entity;

namespace Job.Web.userControls.Personal
{
    public partial class ProjectExperience : BaseCtrl
    {
        int _resumeId = myR["resumeid"].IntValue;
        int _proid = myR["proid"].IntValue;
        int subjectid = myR["subjectid"].IntValue;

        PersonalLogic pl = new PersonalLogic();
        private void ShowMessage(string message)
        {
            this.message.InnerHtml = "<font color=red>" + message + "</font>";
            this.message.Visible = true;
            this.message.Height = "30px";
            this.message.VAlign = "middle";
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.InitData();
                this.LoadData();
            }
        }

        private void InitData()
        {
            //初始化月份
            ddlFMonth.Items.Clear();
            ddlFYear.Items.Clear();
            ddlTYear.Items.Clear();
            ddlTMonth.Items.Clear();

            for (int i = 1; i <= 12; i++)
            {
                ddlFMonth.Items.Add(new ListItem(i.ToString(), i.ToString()));
                ddlTMonth.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

            //初始化年份
            for (int i = 1949; i <= DateTime.Now.Year; i++)
            {
                //ListItem item = new ListItem(i.ToString(), i.ToString());
                ddlFYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                ddlTYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

        }

        private void LoadData()
        {
            try
            {
                JobPersonProject pro = pl.GetProject(_proid);
                if (pro != null)
                {
                    lblMode.Text = "编辑项目经验";
                    txtHard.Text = pro.HardEnvironment.Trim();
                    txtSoft.Text = pro.SoftEnvironment.Trim();
                    txtProjectName.Text = pro.ProjectName.Trim();
                    txtTool.Text = pro.DevTool.Trim();
                    txtPDESC.Text = pro.ProjectDesc.Trim();
                    txtRDESC.Text = pro.ResponseDesc.Trim();
                    cbShow.Checked = pro.ShowInResume.HasValue == true ? pro.ShowInResume.Value : false;
                    if (pro.StartYear.HasValue)
                    {
                        if (ddlFYear.Items.FindByValue(pro.StartYear.Value.ToString()) != null)
                        {
                            ddlFYear.SelectedValue = pro.StartYear.Value.ToString();
                        }
                    }

                    if (pro.StartMonth.HasValue)
                    {
                        if (ddlFMonth.Items.FindByValue(pro.StartMonth.Value.ToString()) != null)
                        {
                            ddlFMonth.SelectedValue = pro.StartMonth.Value.ToString();
                        }
                    }

                    if (pro.EndYear.HasValue)
                    {
                        if (ddlTYear.Items.FindByValue(pro.EndYear.Value.ToString()) != null)
                        {
                            ddlTYear.SelectedValue = pro.EndYear.Value.ToString();
                        }
                    }

                    if (pro.EndMonth.HasValue)
                    {
                        if (ddlTMonth.Items.FindByValue(pro.EndMonth.Value.ToString()) != null)
                        {
                            ddlTMonth.SelectedValue = pro.EndMonth.Value.ToString();
                        }
                    }

                }
                else
                {
                    lblMode.Text = "新增项目经验";
                }

                gvProject.DataSource = pl.GetProjectList(_resumeId);
                gvProject.DataBind();
            }
            catch (Exception ex)
            {
                ShowMessage("查询失败<br>" + ex.Message);
                Logger.WriteLog("项目经验查询失败\r\n" + ex.Message);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                JobPersonProject pro = new JobPersonProject();
                pro.StartYear = Convert.ToInt32(ddlFYear.SelectedValue);
                pro.StartMonth = Convert.ToInt32(ddlFMonth.SelectedValue);
                pro.EndYear = Convert.ToInt32(ddlTYear.SelectedValue);
                pro.EndMonth = Convert.ToInt32(ddlTMonth.SelectedValue);
                pro.SoftEnvironment = txtSoft.Text.Trim();
                pro.HardEnvironment = txtHard.Text;
                pro.ProjectDesc = txtPDESC.Text;
                pro.ResponseDesc = txtRDESC.Text;
                pro.DevTool = txtTool.Text;
                pro.PersonInfoId = _resumeId;
                pro.PersonProjectId = _proid;
                pro.ProjectName = txtProjectName.Text.Trim();
                pro.ShowInResume = cbShow.Checked;
                if (_proid > 0)
                {
                    pl.UpdateProject(pro);
                }
                else
                {
                    pl.InsertProject(pro);
                }
            }
            catch (Exception ex)
            {
                ShowMessage("项目经验保存失败:<br>" + ex.Message);
                Logger.WriteLog("项目经验保存失败:\r\n" + ex.Message);
                return;
            }
            string url = WebCommon.GetApplicationPath(Request) + "Person/PersonalResume.aspx?categoryid=1&subjectid=" + this.subjectid + "&resumeid=" + this._resumeId;
            Response.Redirect(url);
        }

        protected void gvProject_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName.ToLower() == "edit")
            {
                //edit
                string url = WebCommon.GetApplicationPath(Request) + "Person/PersonalResume.aspx?categoryid=1&subjectid=" + this.subjectid + "&resumeid=" + this._resumeId + "&proid=" + id;
                Response.Redirect(url);

            }
            else if (e.CommandName.ToLower() == "delete")
            {
                JobPersonProject _pro = pl.GetProject(id);
                pl.DeleteProject(_pro);

                string url = WebCommon.GetApplicationPath(Request) + "Person/PersonalResume.aspx?categoryid=1&subjectid=" + this.subjectid + "&resumeid=" + this._resumeId;
                Response.Redirect(url);
            }
        }

        protected void gvProject_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }
    }
}